"use strict";

import Vue from 'vue';
import axios from "axios";
import AliLogTracker from './Tracker';
import Agent from './Agent';

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

let config = {
  xsrfCookieName: 'csrfToken',
  xsrfHeaderName: 'x-csrf-token',
};

export const _axios = axios.create(config);

_axios.interceptors.request.use(
  function (settings) {
    return settings;
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
_axios.interceptors.response.use(
  function ({ config, data }) {
    if (process.env.NODE_ENV === 'production') {
      const host = 'cn-qingdao.log.aliyuncs.com';
      const project = 'htetest1';
      const logstore = 'htenewweblog';
      let log = new AliLogTracker(host, project, logstore);
      if (config.data) {
        log.push('_params_', config.data);
      }
      log.push('_method_', config.url);
      log.push('_type_', config.type);
      log.push('_topic_', 'htewebhome');
      new Agent(log);
      log.logger()
    }
    return data;
  },
  function (error) {
    console.log(error)
    // Do something with response error
    return Promise.reject(error);
  }
);

Plugin.install = function (Vue) {
  Vue.axios = _axios;
  window.axios = _axios;
  Object.defineProperties(Vue.prototype, {
    axios: {
      get() {
        return _axios;
      }
    },
    $axios: {
      get() {
        return _axios;
      }
    },
  });
};

Vue.use(Plugin)

export default Plugin;


